const menu = require("express").Router()
const {rules,valid} = require("../../utils/rules")
const nanoid = require("nanoid").nanoid
const {save, rows, sqlList, sqlRows, sqlTotal, sqlDelete, sqlUpdate} = require("../../sql/rows")
const {listToTree} = require("../../utils");
// 添加
menu.post("/save",rules([
    {
        name: "menu_name",
        message: "菜单名称"
    },
    {
        name: "path",
        message: ""
    },
    {
        name: "url",
        message: ""
    },
    {
        name: "menu_type",
        message: ""
    },
    {
        name: "menu_hidden",
        message: ""
    },
    {
        name: "order",
        message: ""
    },
    {
        name: "parent_id",
        message: ""
    }
]), valid, async (req, res) => {
    try {
        const {menu_name, path, url, menu_type, menu_hidden,order, parent_id} = req.body
        // await req.db.query(`INSERT INTO ums_menu (menu_id, menu_name, menu_order, menu_path, menu_url, menu_type, menu_hidden, parent_id, menu_icon, menu_remark) VALUES ('${nanoid()}', '${req.body.menu_name}' , '${req.body.order}', '${req.body.path}', '${req.body.url}', '${req.body.menu_type}', '${req.body.menu_hidden}', '${parent_id || ''}', '', '')`)
        await req.db.query(save('ums_menu', {
            menu_id: nanoid(),
            menu_name,
            menu_order: order,
            menu_path: path,
            menu_url: url,
            menu_type,
            menu_hidden,
            parent_id: parent_id || 0
        }))
        return res.status(200).send({
            code: 200,
            data: "",
            message: "添加成功"
        })
    }catch (e) {
        console.log(e)
        return res.status(500).send({
            code: 500,
            data: "",
            message: "系统错误"
        })
    }
})

// 更新
menu.post("/update", rules([
    {
        name: "menu_name",
        message: "菜单名称"
    },
    {
        name: "path",
        message: ""
    },
    {
        name: "url",
        message: ""
    },
    {
        name: "menu_type",
        message: ""
    },
    {
        name: "menu_hidden",
        message: ""
    },
    {
        name: "order",
        message: ""
    },
    {
        name: "parent_id",
        message: ""
    }
]), valid, async (req, res) => {
    console.log(req.body)
    try {
        const id = req.body.id
        const {menu_name, path, url, menu_type, menu_hidden,order, parent_id} = req.body
        const data = {
            menu_name,
            menu_order: order,
            menu_path: path,
            menu_url: url,
            menu_type,
            menu_hidden,
            parent_id: parent_id
        }
        const sql = sqlUpdate('ums_menu', {data, key:{id}})
        await req.db.query(sql)
        return res.status(200).send({
            code: 200,
            message: "修改成功",
            data: ""
        })
    } catch (e) {
        console.log(e)
        return res.status(500).send({
            code: 500,
            message: "系统错误",
            data: ""
        })
    }
})
//删除
menu.delete("/delete", async (req, res) => {
    try {
        if (req.query.id) {
            const menuId = req.query.id
            const sql = sqlRows('ums_role_or_menu', {data: {menuId}})
            const menus = await req.db.query(sql)
            if (menus.length) {
                const data = {
                    menuId
                }
                await req.db.query(sqlDelete('ums_role_or_menu', {data}))
            }
            const menu = await req.db.query(sqlRows('ums_menu', {data: {menu_id: menuId}}))
            if (menu.length) {
                const data = {
                    menu_id: menuId
                }
                await req.db.query(sqlDelete('ums_menu', {data}))
                return res.status(200).send({
                    code: 200,
                    data: "",
                    message: "删除成功"
                })
            }
            return res.status(200).send({
                code: 400,
                message: '当前列表没有该菜单',
                data: ''
            })
        }
        return res.status(200).send({
            code: 400,
            message: '请传入ID',
            data: ''
        })
    }catch (e) {
        console.log(e)
        return res.status(500).send({
            code: 500,
            data: "",
            message: "系统错误"
        })
    }
})
//查询
menu.post("/list", async (req, res) => {
    const {current, pageSize, data} = req.body
    try {
        const [total] = await req.db.query(sqlTotal('ums_menu'))
        if (data.parent_id === '0') {
            const sql = sqlList('ums_menu', {
                LIMIT: pageSize,
                OFFSET: (current - 1) * 10,
                data
            })
            const row = await req.db.query(sql)
            return res.status(200).send({
                code: 200,
                message: "",
                data: {
                    current: current,
                    pageSize: pageSize,
                    list: row,
                    total: total["COUNT(*)"]
                },
            })
        }else {
            const sql = sqlRows('ums_menu', {
                data
            })
            const row = await req.db.query(sql)
            return res.status(200).send({
                code: 200,
                message: "",
                data: {
                    current: current,
                    pageSize: pageSize,
                    list: row,
                    total: total["COUNT(*)"]
                },
            })
        }

    } catch (e) {
        return res.status(500).send({
            code: 500,
            data: "",
            message: "系统错误"
        })
    }
})

//查询树形菜单表
menu.get("/parentList", async (req, res) => {
    try {
        const row = await req.db.query(rows('ums_menu'))
        return res.status(200).send({
            code: 200,
            data: listToTree(row),
            message: ""
        })
    } catch (e) {
        return res.status(500).send({
            code: 500,
            data: "",
            message: "系统错误"
        })
    }
})

module.exports = menu

